﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class YCAFS05 : DocumentBase
    {
        #region=============Properties=======================
        string Mandant = "00";
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        public OracleConnection OracleDB;
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        YCAFS05Data m_Data;
        INTEXDBC INTEXDBC = null;
        public DataSet setResult;
        YCAFS05Report m_rpt;
        private DataRow dwUI;
        DataTable tableUI;   

        #region==========CollectionViewSource=================
        SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetData");
            }
        }
 
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if (vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        } 

        CollectionViewSource vwVKKOPF
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwVKKOPF");
            }
        } 
        string art
        {
            get
            {
                if (dwVKPOS == null)
                    return "";
                return dwVKPOS["VKPART"].ToString();

            }
        }
        #endregion
        int total = 0;
        public YCAFS05()
        {
            InitializeComponent();
            this.Name = "款号BOM"; 
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;
 
                }
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                Mandant = (string)dicParamater["MANDANT"];

                m_Data = new YCAFS05Data(OracleDB, proxy);
                INTEXDBC = new INTEX.DataBase.Tools.INTEXDBC(OracleDB, proxy); 
                #region============TESTINGDATA================
                tableUI = new DataTable("UIData");
                tableUI.Columns.Add("ARTICLE", typeof(string));
                tableUI.Columns.Add("COLL", typeof(string));
                tableUI.Columns.Add("REASON", typeof(string));
                tableUI.Columns.Add("PRDGRP", typeof(string));
                tableUI.Columns.Add("ARTGRP", typeof(string));
                tableUI.Columns.Add("FART", typeof(string));
                #endregion 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }  
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                    this.dicParamater = dicParamater;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        #endregion
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        /*private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }*/
        private void btnDistribute_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType, true);
        }
        public void GenerateReport(string PrintType, bool isapproved = false)
        {
            try
            {
                YCAFS05Data m_DX = new YCAFS05Data(dicParamater);
                YCAFS05Report m_rpt = new YCAFS05Report(dicParamater);
                m_rpt.dsSource = SetData;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DX.exAgent;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.proxy = this.proxy; 
                m_rpt.WriteExcelReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null, null);

        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
            try
            {
                SetData.Clear();
                DataSet setResult = new DataSet();
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    ReflectionStudio.Controls.GB2312Convert8859P1 gb2312 = new ReflectionStudio.Controls.GB2312Convert8859P1();
                    
                    dwUI = tableUI.NewRow();
                    string ARTICLE = txtARTICLE.Text.Trim();
                    string COLL = txtCOLL.Text.Trim();
                    string REASON = txtREASON.Text.Trim();
                    string PRDGRP = txtPRDGRP.Text.Trim();
                    string ARTGRP = txtARTGRP.Text.Trim();
                    string FART = txtFART.Text.Trim();
                    if (COLL == "")
                    {
                        COLL = "XXXXXX";
                    }
                    if (REASON == "")
                    {
                        REASON = "XXXXXX";
                    }
                    if (PRDGRP == "")
                    {
                        PRDGRP = "XXXXXX";
                    }
                    if (ARTGRP == "")
                    {
                        ARTGRP = "XXXXXX";
                    }
                    dwUI["ARTICLE"] = ARTICLE;
                    dwUI["COLL"] = COLL;
                    dwUI["REASON"] = REASON;
                    dwUI["PRDGRP"] = PRDGRP;
                    dwUI["ARTGRP"] = ARTGRP;
                    dwUI["FART"] = FART;
                    
                    setResult = m_Data.GetData(dwUI);
                    SetData.Merge(setResult); 
                }
                btnReport.IsEnabled = true;
                /*btnPrintPreview.IsEnabled = true;*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            EventDispatcher.Instance.RaiseStatus("查询结束", StatusEventType.StopProgress);
        }

        
    }
}
